import torch
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
# from datasets import load_dataset

device = "cuda:0" if torch.cuda.is_available() else "cpu"
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32

model_id = 'openai/whisper-large-v3-turbo'

print('Loading model...')
model = AutoModelForSpeechSeq2Seq.from_pretrained(model_id,
                                                  torch_dtype=torch_dtype,
                                                  low_cpu_mem_usage=True, use_safetensors=True,cache_dir='data-cache')
model.generation_config.task = 'transcribe'
model.generation_config.language = 'en'  # for hindi
model.to(device)
print('Model loaded.')
processor = AutoProcessor.from_pretrained(model_id)

print('Loading dataset...')
pipe = pipeline(
    'automatic-speech-recognition',
    model=model,
    tokenizer=processor.tokenizer,
    feature_extractor=processor.feature_extractor,
    torch_dtype=torch_dtype,
    device=device,
)


# Create
def get_audio_data(audio_path: str) -> str:
    print('Getting audio data...'+audio_path)
    result =pipe(audio_path, generate_kwargs={'language': 'chinese'})
    print('Audio data got.')
    print(result)
    return result['text']

